package com.fh.service.impl;

import com.fh.mapper.UsersMapper;

import com.fh.model.Users;
import com.fh.service.UsersService;
import com.fh.utils.Base64Utils;
import com.fh.utils.ResultState;
import com.fh.vo.ResultVo;
import io.jsonwebtoken.*;
import io.jsonwebtoken.impl.DefaultJwtBuilder;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UsersServiceImpl implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Override
    public ResultVo login(String username, String userPwd) {

        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        Users user = usersMapper.selectOneByExample(example);
        if (user==null){
          return  new  ResultVo(ResultState.NO,"账号不存在",null);
             }else {
                if (user.getPassword().equals(userPwd)){
                    Map map=new HashMap();
                    JwtBuilder builder=Jwts.builder();
                    String token=builder.setSubject(username)
                            .setIssuedAt(new Date())
                    .setId(String.valueOf(user.getUserId()))
                    .setClaims(map)
                    .setExpiration(new Date(System.currentTimeMillis()+30*1000000))
                     .signWith(SignatureAlgorithm.HS256, "xukun")
                    .compact();
                 return  new  ResultVo(ResultState.OK,token,user);
             }
             else {
                 return  new  ResultVo(501,"密码错误",null);
             }
         }
    }


     public  ResultVo regist(String username ,String password){
         Example example = new Example(Users.class);
         Example.Criteria criteria = example.createCriteria();
         criteria.andEqualTo("username",username);
         Users user = usersMapper.selectOneByExample(example);
      if (user==null){
           Users users=new Users();
          users.setPassword(password);
          users.setUsername(username);
          users.setUserImg("111");
          users.setUserModtime(new Date());
          users.setUserRegtime(new Date());
          int i = usersMapper.insert(users);
         if(i>0){
             return  new ResultVo(ResultState.OK,"注册成功",null);
         }else {
             return  new ResultVo(ResultState.NO,"注册失败",null);
         }
      }else {
          return  new ResultVo(501,"账号重复",null);
      }
     }

    @Override
    public ResultVo query() {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("username","%h%");
        //分⻚查询
        int pageNum = 1;
        int pageSize = 5;
        int start = (pageNum-1)*pageSize;
        RowBounds rowBounds = new RowBounds(start,pageSize);
        List<Users> users = usersMapper.selectByExampleAndRowBounds(example, rowBounds);
        return new ResultVo(200,"11",users);

    }


}
